package org.example;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.junit.Test;

import java.io.File;

public class ImageTest1 {

    @Test
    public void test() throws TesseractException {
        String path = "C:\\Users\\lenovo\\Pictures\\Saved Pictures\\9985.jpg";
        String lagnguagePath = "E:\\OCR\\tessdata";
        File image = new File(path);
        System.out.println(image);
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath(lagnguagePath);
        tesseract.setLanguage("chi_sim");
//        String result =null;
//        try {
        String  result = tesseract.doOCR(image);
//        }catch (TesseractException e){
//            e.printStackTrace();
//        }
        result = result.replace(" ", "").replace("\n", "");


        System.out.println(result);
//        int startIndex = result.indexOf("名称") + "名称".length();
//        int endIndex = result.indexOf("注册资本");
//        String name = result.substring(startIndex, endIndex).trim();
//        int startpeople = result.indexOf("法定代表人") + "法定代表人".length();
//        int endpeople = result.indexOf("营业期限");
////        if (startIndex>0 && endIndex>0 && endIndex>startIndex){
//        String people = result.substring(startpeople, endpeople);
        int startIndex = result.indexOf("姓名") + "姓名".length();
        int endIndex = result.indexOf("性");
        String name = result.substring(startIndex, endIndex).trim();
        System.out.println("提取的姓名："+name);
//            System.out.println(people);
            System.err.println(name);
//        }else {
//            System.out.println("没有找到名称");
//        }
//        String result = tesseract.doOCR(image);
//        String result = tesseract.doOCR(image, new Rectangle(1200, 200));
//        System.out.println("result:");
//        System.out.println(result);
    }
}
